<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录界面</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f5f7fa;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            position: relative;
        }

        #resetButtoon {
            background-color: white;
            border: none;
            padding: 10px 20px;
            font-size: 14px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            border-radius: 6px;
            cursor: pointer;
            position: absolute;
            left: 30px;
            top: 30px;
            color: #333;
            transition: background-color 0.3s ease;
        }
        #resetButtoon:hover {
            background-color: #f5f7fa;
        }

        .container {
            width: 400px;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h1 {
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 6px;
            box-sizing: border-box;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        .form-group input:focus {
            outline: none;
            border-color: #1e66f5;
            box-shadow: 0 0 0 2px rgba(30, 102, 245, 0.1);
        }

        .register-link {
            color: #0066ff;
            text-decoration: none;
            font-size: 14px;
            margin-top: 15px;
            display: inline-block;
        }
        .register-link:hover {
            text-decoration: underline;
        }

        .form-group button {
            width: 100%;
            padding: 14px;
            background-color: #1e66f5;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 20px;
            transition: background-color 0.3s ease;
        }
        .form-group button:hover {
            background-color: #1952d1;
        }

        label {
            display: block;
            margin-bottom: 6px;
            color: #555;
        }

    </style>

</head>
<body>
<button id="resetButtoon" onclick="goBack()">返回首页</button>

<div class="container">
    <h1>用户登录</h1>

    <form>
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required placeholder="请输入用户名" />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required placeholder="请输入密码" />
        </div>

        <p><a href="./register.html" class="register-link">没有账号？去注册</a></p>

        <div class="form-group">
            <button type="submit">登录</button>
        </div>
    </form>
</div>


<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
    function goBack() {
        window.location.href = "home.html";
    }
    function goRegister() {
        window.location.href = "register.html";
    }
    $(function(){
        $('form').on('submit', function(event) {
            event.preventDefault(); // 阻止表单默认提交行为
            onSubmit();
        });
    }
    )
    const onSubmit =async()=>{
        console.log("提交表单");
    let username = $('#username');
    let password = $('#password');
    if(username.val().length===0){
        alert("用户名不能为空");
        return ;}
    if(password.val().length===0){
        alert("密码不能为空");
        return ;
    }
    let data = {
        userName:username.val(),
        password:password.val()};
        $('[type=submit]').prop('disabled', true).text('登录中...');
    $.ajax({
        type: "POST",
        url: "/login",
        headers: {
            "Content-Type": "application/json",
        },
        data: JSON.stringify(data),
        success: function(response) {
            if(response.code===200){
                if(response.data.token){
                    localStorage.setItem("token",response.data.token);
                }
                alert("登录成功");
                if(response.data.role=="admin"){
                    window.location.href = "admin/adminHome.html";
                }else {
                    window.location.href = "home.html";
                }
            }else{
                alert(response.message);
            }
        },
        error: function(xhr, status, error) {
            alert("请求失败，请稍后重试");
        },
        complete: function() {
            $('[type=submit]').prop('disabled', false).text('登录');
        }
    })
    }
</script>
</body>
</html>